package com.adingxiong.cft.design.servicelocation;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Cache
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/1/8 9:28
 **/
public class Cache {

    private List<Service> services;

    public Cache(){
        services = new ArrayList<>();
    }

    public Service getService(String serviceName) {
        for (Service service : services) {
            if(service.getName().equalsIgnoreCase(serviceName)){
                System.out.println("Returning cached :" + serviceName + "object");
                return service;
            }
        }
        return null;
    }


    public void addService(Service newService) {
        boolean exsit = false;
        for (Service service:services){
            if(service.getName().equalsIgnoreCase(newService.getName())){
                exsit = true;
            }
        }
        if(!exsit){
            services.add(newService);
        }
    }
}
